/**
 * 卡片数据本地存储工具类
 */

export interface Card {
  id: string;
  title: string;
  content: string;
  tags: string[];
  startTime?: string;
  createdAt: string;
  updatedAt: string;
}

const CARDS_STORAGE_KEY = 'cards';

/**
 * 获取所有卡片
 */
export function getAllCards(): Card[] {
  try {
    const cardsStr = localStorage.getItem(CARDS_STORAGE_KEY);
    return cardsStr ? JSON.parse(cardsStr) : [];
  } catch (error) {
    console.error('Failed to get cards from localStorage:', error);
    return [];
  }
}

/**
 * 获取单个卡片
 * @param id 卡片ID
 */
export function getCardById(id: string): Card | null {
  try {
    const cards = getAllCards();
    return cards.find(card => card.id === id) || null;
  } catch (error) {
    console.error(`Failed to get card with id ${id}:`, error);
    return null;
  }
}

/**
 * 保存卡片
 * @param card 卡片数据
 */
export function saveCard(card: Card): boolean {
  try {
    const cards = getAllCards();
    const index = cards.findIndex(c => c.id === card.id);
    
    if (index >= 0) {
      // 更新现有卡片
      cards[index] = card;
    } else {
      // 添加新卡片
      cards.push(card);
    }
    
    localStorage.setItem(CARDS_STORAGE_KEY, JSON.stringify(cards));
    return true;
  } catch (error) {
    console.error('Failed to save card:', error);
    return false;
  }
}

/**
 * 删除卡片
 * @param id 卡片ID
 */
export function deleteCard(id: string): boolean {
  try {
    const cards = getAllCards();
    const filteredCards = cards.filter(card => card.id !== id);
    localStorage.setItem(CARDS_STORAGE_KEY, JSON.stringify(filteredCards));
    return true;
  } catch (error) {
    console.error(`Failed to delete card with id ${id}:`, error);
    return false;
  }
}

/**
 * 生成唯一ID
 */
export function generateUniqueId(): string {
  return Date.now().toString() + Math.random().toString(36).substr(2, 9);
}

/**
 * 初始化示例卡片数据
 */
export function initSampleCards(): void {
  // 仅当本地存储中没有卡片数据时初始化
  if (getAllCards().length > 0) {
    return;
  }
  
  const now = new Date();
  const yesterday = new Date(now);
  yesterday.setDate(now.getDate() - 1);
  
  const lastWeek = new Date(now);
  lastWeek.setDate(now.getDate() - 7);
  
  const nextWeek = new Date(now);
  nextWeek.setDate(now.getDate() + 7);
  
  const sampleCards: Card[] = [
    {
      id: '1',
      title: '项目启动会议',
      content: '讨论项目目标、范围和时间线',
      tags: ['会议', '项目管理'],
      startTime: yesterday.toISOString(),
      createdAt: lastWeek.toISOString(),
      updatedAt: yesterday.toISOString()
    },
    {
      id: '2',
      title: '产品设计评审',
      content: '评审UI/UX设计方案，确定最终设计',
      tags: ['设计', '评审'],
      startTime: now.toISOString(),
      createdAt: lastWeek.toISOString(),
      updatedAt: now.toISOString()
    },
    {
      id: '3',
      title: '开发任务分配',
      content: '为团队成员分配开发任务和职责',
      tags: ['开发', '任务管理'],
      startTime: nextWeek.toISOString(),
      createdAt: yesterday.toISOString(),
      updatedAt: yesterday.toISOString()
    },
    {
      id: '4',
      title: '每周进度回顾',
      content: '回顾本周进度，讨论遇到的问题和解决方案',
      tags: ['会议', '进度回顾'],
      startTime: nextWeek.toISOString(),
      createdAt: now.toISOString(),
      updatedAt: now.toISOString()
    },
    {
      id: '5',
      title: '客户需求收集',
      content: '与客户会面，收集和确认需求',
      tags: ['需求', '客户'],
      createdAt: lastWeek.toISOString(),
      updatedAt: lastWeek.toISOString()
    }
  ];
  
  localStorage.setItem(CARDS_STORAGE_KEY, JSON.stringify(sampleCards));
} 